package com.kvest.sherlocktest.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.view.*;
import android.view.MenuItem;
import android.widget.TextView;
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.Menu;
import com.kvest.sherlocktest.R;

/**
 * Created with IntelliJ IDEA.
 * User: r.belokon
 * Date: 15.04.13
 * Time: 14:07
 * To change this template use File | Settings | File Templates.
 */
public class Fragment1 extends SherlockFragment {
    public static final String ARGUMENT_TEXT = "text";
    public static final String ARGUMENT_COLOR = "color";
    public static final String ARGUMENT_HAS_OPTIONS_MENU = "has_options_menu";
    public static final String ARGUMENT_MENU_TEXT = "menu_text";

    public static Fragment1 getFragment(String text, int color, boolean hasOptionsMenu, String menuText) {
        Bundle arguments = new Bundle();
        arguments.putString(ARGUMENT_TEXT, text);
        arguments.putInt(ARGUMENT_COLOR, color);
        arguments.putBoolean(ARGUMENT_HAS_OPTIONS_MENU, hasOptionsMenu);
        arguments.putString(ARGUMENT_MENU_TEXT, menuText);


        Fragment1 result = new Fragment1();
        result.setArguments(arguments);

        return result;
    }

    private String text;
    private int color;
    private boolean hasOptionsMenu;
    private String menuText;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Bundle arguments = getArguments();
        if (arguments != null) {
            text = arguments.getString(ARGUMENT_TEXT);
            color = arguments.getInt(ARGUMENT_COLOR);
            hasOptionsMenu = arguments.getBoolean(ARGUMENT_HAS_OPTIONS_MENU);
            menuText = arguments.getString(ARGUMENT_MENU_TEXT);
        } else {
            text = "Unknown text";
            color = Color.RED;
            hasOptionsMenu = true;
            menuText = "menuT";
        }

        setHasOptionsMenu(hasOptionsMenu);
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        TextView tv = new TextView(getActivity());
        tv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        tv.setText(text);
        tv.setBackgroundColor(color);

        return tv;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);

        menu.add(menuText).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    }
}
